﻿package  
{
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	
	//currently all obstacles must be spawned on screen otherwise they will be immediately 
	//remove - Change it so there is a padding of maybe half or a third of screen width
	
	import flash.events.TimerEvent;
	import flash.display.Sprite;
	import Box2D.Common.Math.b2Vec2;
	public class Obstacle extends Sprite implements gameObject
	{
		//protected var _timer:FrameTimer;
		public var position:b2Vec2;
		public var _animation:AnimatedSprite;
		protected var bHit:Boolean = false;
		private var bAlive:Boolean = true;
		private var bDead:Boolean = false;
		private var checkColls:Boolean = true;
		public function Obstacle(X:Number, Y:Number) 
		{
			position = new b2Vec2(X, Y);
			
			this.position.x = X;
			this.position.y = Y;
			//this._timer = timer;
			
			
			//_timer.addEventListener(TimerEvent.TIMER, Update, false, 0, true);
			
			//this._animation = animation;
			this.x = this.position.x * 30;
			this.y = this.position.y * 30;
			

			
		
			

		}
		
		public function initalise(timer:FrameTimer,X:Number, Y:Number, animation:AnimatedSprite):void
		{
			this.position.x = X;
			this.position.y = Y;
			//this._timer = timer;
			
			
			//_timer.addEventListener(TimerEvent.TIMER, Update, false, 0, true);
			
			this._animation = animation;
			this.x = this.position.x * 30;
			this.y = this.position.y * 30;
			

			
			this.addChild(animation);
		}
		public function Update():Boolean
		{
			this._animation.Update();
			
			////////DEBUG CODE////////////////////////////
/*			this.graphics.clear();
			this.graphics.beginFill(0xff0000);
			this.graphics.drawCircle(0, 0, this._animation.width/2);
			this.graphics.endFill();
			*/
			//////////////////////////////////////////////
			
			this._animation.x = -this._animation.width / 2;
			this._animation.y = -this._animation.height / 2;
			
			
			if (checkCollision())
			{
				if (bAlive)
				{
					Dispose();
					bAlive = false;
				
				}
				
			}

			if (bDead)
			{
				//this.die();
				return false;
			}
			return true;
		}
		
		private function checkCollision():Boolean
		{
			if(((World.playerPosition.x - World.playerWidth/2/30) < this.position.x + this.width / 2/30)
			&&((World.playerPosition.x) + World.playerWidth/2/30 > this.position.x - this.width / 2/30)
			&&((World.playerPosition.y - World.playerWidth/2/30) < this.position.y + this.height / 2/30)
			&&((World.playerPosition.y + World.playerWidth/2/30) > this.position.y - this.height / 2/30)
			  )
			  {
				  if (checkColls == true)
				  {
					World.bPlayerHit = true;
					checkColls = false;
				  }
				  return true;
			  }
			  else
			  {
				  return false;
			  }
			
		}
		
		public function CheckOnScreen():Boolean
		{
				if(((this.position.x*30 > World.cameraPosition.x*30 
				&&this.position.x * 30 < World.cameraPosition.x * 30 +Constants.WIDTH))
				||
				((this.position.y*30 > World.cameraPosition.y*30 
				&&this.position.y * 30 < World.cameraPosition.y * 30 +Constants.HEIGHT)

				))
				{
					return false;
				}
				else
				{
					
					return true;
				}
		}
		
		public function Dispose():void
		{
			//kill the timer and remove the event listener to let the garbage collection kick in
			//this._timer.removeEventListener(TimerEvent.TIMER, Update);
			
			//can't call stop on this it is just a reference to another timer
			//this._timer.stop();
			//this._timer = null;
			
			//clean up the animated sprite
			//this._animation.Dispose();
			//this._animation = null;
			Animation(this._animation.Animations[this._animation.CurrentAnimation]).bDying = true;
			//this.parent.removeChild(this);
		}
		
				//function that will completely dispose of this pickup (any type) and remove it from it's parent's
		//display list
		public function die():void
		{
			this._animation.Dispose();
			this.removeChild(_animation);
			this.parent.removeChild(this);
			this.bDead = true;
		}
		
		
	}
	
}